({
    data: {
        resultdata: {},
        dbConn: Program.data.dbConn,
        WorkId: Program.data.WorkId,
        requestBody: Program.data.requestBody,
        ExcuteRecordModel: {
            model: {
                RecordId: 0,
                MemberId: 0,
                TaskCode: "",
                TaskName: "",
                ExcuteStatus: 0,
                ExcuteTime: "yyyy-MM-dd HH:mm:ss",
                Memo: "",
                WorkId: 0,
                OrderNo: ""
            },
            tablename: "Me_ExcuteRecord",
            key: "RecordId",
            val: "0",
            objval: {},
            WorkId: LobsterAPI.ProgramData('workid')
        },
        PointRecordModel: {
            model: {
                RecordId: 0,
                MemberId: 0,
                TaskCode: "",
                TaskName: "",
                RecordTime: "yyyy-MM-dd HH:mm:ss",
                Value: 0,
                Memo: "",
                WorkId: 0,
                OrderNo: ""
            },
            tablename: "Me_PointRecord",
            key: "RecordId",
            val: "0",
            objval: {},
            WorkId: LobsterAPI.ProgramData('workid')
        },
        GrowthRecordModel: {
            model: {
                RecordId: 0,
                MemberId: 0,
                TaskCode: "",
                TaskName: "",
                RecordTime: "yyyy-MM-dd HH:mm:ss",
                Value: 0,
                Memo: "",
                WorkId: 0,
                OrderNo: ""
            },
            tablename: "Me_GrowthRecord",
            key: "RecordId",
            val: "0",
            objval: {},
            WorkId: LobsterAPI.ProgramData('workid')
        },
        BasicInfoModel: {
            model: {
                MemberId: 0,
                NickName: "",
                "[Level]": 0,
                MemberPoints: 0,
                UserId: "",
                CouponCounts: 0,
                GrowthValue: 0,
                WorkId: 0,
            },
            tablename: "Me_BasicInfo",
            key: "MemberId",
            val: "0",
            objval: {},
            WorkId: LobsterAPI.ProgramData('workid')
        },
        CouponRecordModel: {
            model: {
                RecordId: 0,
                CouponId: 0,
                CouponCode: "",
                CouponName: "",
                MemberId: 0,
                CouponClass: 0,
                CouponType: 0,
                StartTime: "yyyy-MM-dd HH:mm:ss",
                EndTime: "yyyy-MM-dd HH:mm:ss",
                CouponAmount: 0,
                CouponDiscount: 0,
                FullImpairment: 0,
                CouponStatus: 0,
                AdaptiveRange: 0,
                WorkId: 0,
                GetTime: "yyyy-MM-dd HH:mm:ss",
                OrderNo: "",
                LevelId: 0,
                RightId: 0,
            },
            tablename: "Me_CouponRecord",
            key: "RecordId",
            val: "0",
            objval: {},
            WorkId: LobsterAPI.ProgramData('workid')
        }
    },
    //实现增删改查示例API控制器
    main: function (funname) {
        var self = this;
        self.data.dbConn = PublicFunction.setdbconn('10.0.4.5:1433', 'CloudMember', 'sa', 'Lobster@123');
        LobsterAPI.TrackLog("1111111111", funname);
        //查询
        if (funname == "member.getMemberInfo") {
            var userId = LobsterAPI.RequestQuery("userId", 1);

            self.data.resultdata = self.getMemeberInfo(userId);
        }
        else if (funname == "member.getPointRecord") {
            var page = LobsterAPI.RequestQuery("page", 1);
            var limit = LobsterAPI.RequestQuery("limit", 10);
            var userId = LobsterAPI.RequestQuery("userId", 1);
            self.data.resultdata = self.getPointRecord(page, limit, userId);
        } else if (funname == "member.getLevelRight") {
            var userId = LobsterAPI.RequestQuery("userId", 1);
            self.data.resultdata = self.getLevelRight(userId);
        } else if (funname == "member.getGrowthRecord") {
            LobsterAPI.TrackLog("member.getGrowthRecord", "member.getGrowthRecord");
            var page = LobsterAPI.RequestQuery("page", 1);
            var limit = LobsterAPI.RequestQuery("limit", 10);
            var userId = LobsterAPI.RequestQuery("userId", 1);
            self.data.resultdata = self.getGrowthRecord(page, limit, userId);
        } else if (funname == "member.getRightCoupon") {
            var userId = LobsterAPI.RequestQuery("userId", 1);
            self.data.resultdata = self.getRightCoupon(userId);
        }
        else if (funname == "member.registermember") {
            var model = JSON.parse(self.data.requestBody);
            self.data.resultdata = self.registermember(model);
        } else if (funname == "member.bonus") {
            var model = JSON.parse(self.data.requestBody);
            self.data.resultdata = self.bonus(model);
        } else if (funname == "member.obtainCoupon") {
            var model = JSON.parse(self.data.requestBody);
            self.data.resultdata = self.obtainCoupon(model);
        } else if (funname == "member.getcouponlist") {
            var page = LobsterAPI.RequestQuery("page", 1);
            var limit = LobsterAPI.RequestQuery("limit", 10);
            var userId = LobsterAPI.RequestQuery("userId", 1);
            self.data.resultdata = self.getcouponlist(page, limit, userId);
        }
        return self.data.resultdata;
    },
    getMemeberInfo: function (userId) {
        var strsql = "select * from Me_BasicInfo where UserId=? and WorkId=?";
        var dbparams = Packages.java.util.ArrayList();
        LobsterAPI.TrackLog("2222222222222", userId);
        dbparams.add(userId);
        dbparams.add(self.data.WorkId);
        var basicInfo = ORMFunction.GetJsonObject(self.data.dbConn, strsql, dbparams);
        LobsterAPI.TrackLog("2222222222222", basicInfo);
        if (!basicInfo.MemberId) {
            return { code: 1, msg: "不存在会员数据" };
        }
        return { code: 0, data: { basicInfo: basicInfo } }
    },
    getPointRecord(page, limit, userId) {
        var self = this;
        var pageInfo = {};
        pageInfo.key = "RecordTime desc";
        pageInfo.page = page;
        pageInfo.perPage = limit;
        pageInfo.totalRecord = 0;
        pageInfo.totalPage = 0;
        var strsql = "select a.* from Me_PointRecord a \
        inner join Me_BasicInfo b\
        on a.MemberId=b.MemberId\
        where b.WorkId=? and b.UserId=? ";
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(self.data.WorkId);
        dbparams.add(userId);
        var data = ORMFunction.GetJsonData(self.data.dbConn, strsql, dbparams, pageInfo);
        return { code: 0, data: data, count: pageInfo.totalRecord };
    },
    getLevelRight(userId) {
        var self = this;
        var strsql = "select * from Me_BasicInfo where WorkId=? and UserId=?";
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(self.data.WorkId);
        dbparams.add(userId);
        var basicInfo = ORMFunction.GetJsonObject(self.data.dbConn, strsql, dbparams);
        strsql = "select * from Me_MemberLevel where WorkId=?";
        dbparams = Packages.java.util.ArrayList();
        dbparams.add(self.data.WorkId);
        var memberlevelList = ORMFunction.GetJsonData(self.data.dbConn, strsql, dbparams);
        strsql = "select * from Me_LevelRight where WorkId=?";
        dbparams = Packages.java.util.ArrayList();
        dbparams.add(self.data.WorkId);
        var rightList = ORMFunction.GetJsonData(self.data.dbConn, strsql, dbparams);
        strsql = "select a.*,c.MemberLevelId from Me_LevelRight a\
        left join Me_LevelRightRelation b\
        on a.RightId=b.RightId\
        left join Me_MemberLevel c\
        on b.LevelId=c.MemberLevelId\
        where a.WorkId=?";
        dbparams = Packages.java.util.ArrayList();
        dbparams.add(self.data.WorkId);
        var memberrightList = ORMFunction.GetJsonData(self.data.dbConn, strsql, dbparams);
        return {
            code: 0, data: {
                baseinfo: basicInfo,
                memberlevelList: memberlevelList,
                rightList: rightList,
                memberrightList: memberrightList
            }
        }
    },
    getGrowthRecord(page, limit, userId) {
        var self = this;
        var pageInfo = {};
        pageInfo.key = "RecordTime desc";
        pageInfo.page = page;
        pageInfo.perPage = limit;
        pageInfo.totalRecord = 0;
        pageInfo.totalPage = 0;
        var strsql = "select a.* from Me_GrowthRecord a \
        inner join Me_BasicInfo b\
        on a.MemberId=b.MemberId\
        where b.WorkId=? and b.UserId=? ";
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(self.data.WorkId);
        dbparams.add(userId);
        LobsterAPI.TrackLog("getGrowthRecord1", page);
        var data = ORMFunction.GetJsonData(self.data.dbConn, strsql, dbparams, pageInfo);
        LobsterAPI.TrackLog("getGrowthRecord2", "sss");
        return { code: 0, data: data, count: pageInfo.totalRecord };
    },
    getRightCoupon() {
        var self = this;
        var strsql = "select * from Me_MemberLevel where WorkId=?";
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(self.data.WorkId);
        var levelList = ORMFunction.GetJsonData(self.data.dbConn, strsql, dbparams);
        strsql = "select * from Me_LevelRight where WorkId=?";
        dbparams = Packages.java.util.ArrayList();
        dbparams.add(self.data.WorkId);
        var rightList = ORMFunction.GetJsonData(self.data.dbConn, strsql, dbparams);
        strsql = "select a.*,c.MemberLevelId from Me_LevelRight a\
        left join Me_LevelRightRelation b\
        on a.RightId=b.RightId\
        left join Me_MemberLevel c\
        on b.LevelId=c.MemberLevelId\
        where a.WorkId=?";
        dbparams = Packages.java.util.ArrayList();
        dbparams.add(self.data.WorkId);
        var memberrightList = ORMFunction.GetJsonData(self.data.dbConn, strsql, dbparams);
        strsql = "select b.LevelId,b.RightId,  a.CouponId,a.CouponName from Me_Coupon a\
        inner join Me_RightCouponRelation b\
        on a.CouponId=b.CouponId\
        inner join Me_MemberLevel c\
        on b.LevelId=c.MemberLevelId\
        where a.WorkId=? and a.CouponStatus=2\
        and ((a.StartTime>GETDATE() and a.ValidType=1) or a.ValidType=2)";
        dbparams = Packages.java.util.ArrayList();
        dbparams.add(self.data.WorkId);
        var membercouponList = ORMFunction.GetJsonData(self.data.dbConn, strsql, dbparams);
        return {
            code: 0, data: {
                levelList: levelList,
                rightList: rightList,
                memberrightList: memberrightList,
                membercouponList: membercouponList,
            }
        }
    },
    bonus(model) {
        var self = this;
        try {
            self.data.dbConn.setAutoCommit(false)
            var strsql = "select * from Me_Task where WorkId=? and TaskCode=?";
            var dbparams = Packages.java.util.ArrayList();
            dbparams.add(self.data.WorkId);
            dbparams.add(model.TaskCode);
            var task = ORMFunction.GetJsonObject(self.data.dbConn, strsql, dbparams);
            if (!task.TaskId)
                return { code: 1, msg: "找不到任务" };
            strsql = "select * from Me_BasicInfo where WorkId=? and UserId=?";
            dbparams = Packages.java.util.ArrayList();
            dbparams.add(self.data.WorkId);
            dbparams.add(model.UserId);
            var info = ORMFunction.GetJsonObject(self.data.dbConn, strsql, dbparams);
            LobsterAPI.TrackLog("会员信息", info);
            if (!info.MemberId)
                return { code: 1, msg: "找不到会员信息" };
            if (task.TaskType == 1) {
                strsql = "select * from Me_ExcuteRecord where WorkId=? and MemberId=? and TaskCode=? and ExcuteStatus=1";
                dbparams = Packages.java.util.ArrayList();
                dbparams.add(self.data.WorkId);
                dbparams.add(info.MemberId);
                dbparams.add(model.TaskCode);
                var record = ORMFunction.GetJsonObject(self.data.dbConn, strsql, dbparams);
                if (record.RecordId > 0)
                    return Error("已经奖励，不重复奖励");
            } else if (task.TaskType == 2) {
                var self = this;
                var recorddate = self.getdate()
                strsql = "select sum(value) as total from Me_PointRecord where WorkId=? and MemberId=? and TaskCode=? and  CONVERT(varchar(20), RecordTime,23)=?";
                dbparams = Packages.java.util.ArrayList();
                dbparams.add(self.data.WorkId);
                dbparams.add(info.MemberId);
                dbparams.add(model.TaskCode);
                dbparams.add(recorddate);
                var pointRecord = ORMFunction.GetJsonObject(self.data.dbConn, strsql, dbparams);
                strsql = "select sum(value) as total from Me_GrowthRecord where WorkId=? and MemberId=? and TaskCode=? and  CONVERT(varchar(20), RecordTime,23)=?";
                dbparams = Packages.java.util.ArrayList();
                dbparams.add(self.data.WorkId);
                dbparams.add(info.MemberId);
                dbparams.add(model.TaskCode);
                dbparams.add(recorddate);
                var growthRecord = ORMFunction.GetJsonObject(self.data.dbConn, strsql, dbparams);
                var bonusPoints = 0;
                var bonusGrowth = 0;
                LobsterAPI.TrackLog("任务类型为2pointRecord", pointRecord);
                LobsterAPI.TrackLog("任务类型为2growthRecord", growthRecord);
                if (pointRecord.total) {
                    bonusPoints = parseInt(pointRecord.total);
                }
                if (growthRecord.total) {
                    bonusGrowth = parseInt(growthRecord.total);
                }

                if (bonusPoints + parseInt(task.BonusPoints) > parseInt(task.BonusPointsLimit)) {
                    task.BonusPoints = 0;
                }
                if (bonusGrowth + parseInt(task.BonusGrowth) > parseInt(task.BonusGrowthLimit)) {
                    task.BonusGrowth = 0;
                }
            } else if (task.TaskType == 3) {
                strsql = "select * from Me_BonusRule where TaskId=?";
                dbparams = Packages.java.util.ArrayList();
                dbparams.add(task.TaskId);
                var rule = ORMFunction.GetJsonObject(self.data.dbConn, strsql, dbparams);
                if (!rule.RuleId)
                    return { code: 1, msg: "找不到奖励规则数据" };
                if (rule.RuleType == 1) {
                    task.BonusPoints = Math.round(model.Jine / rule.PointsPercent);
                    task.BonusGrowth = Math.round(model.Jine / rule.GrowthPercent);
                } else if (rule.RuleType == 2) {
                    strsql = "select * from Me_AmountRange where StartAmount<=? and EndAmount>=? and RuleId=?";
                    dbparams = Packages.java.util.ArrayList();
                    dbparams.add(model.Jine);
                    dbparams.add(model.Jine);
                    dbparams.add(rule.RuleId);
                    var range = ORMFunction.GetJsonObject(self.data.dbConn, strsql, dbparams);
                    if (!range.RangeId)
                        return { code: 1, msg: "找不到金额范围数据" };
                    task.BonusPoints = range.BonusPoints;
                    task.BonusGrowth = range.BonusGrowth;
                }
            }
            if (task.BonusPoints == 0 && task.BonusGrowth == 0)
                return { code: 1, msg: "不奖励" };
            var now = self.getdatetime()
            LobsterAPI.TrackLog("记录时间", now);
            self.data.ExcuteRecordModel.objval = {
                MemberId: info.MemberId,
                TaskCode: task.TaskCode,
                TaskName: task.TaskName,
                ExcuteStatus: 1,
                ExcuteTime: now,
                OrderNo: model.OrderNo,
                Memo: model.Memo,
                WorkId: self.data.WorkId
            };
            LobsterAPI.OrmMySql.SaveModel(self.data.dbConn, self.data.ExcuteRecordModel);
            if (task.BonusPoints > 0) {
                self.data.PointRecordModel.objval = {
                    MemberId: info.MemberId,
                    TaskCode: task.TaskCode,
                    TaskName: task.TaskName,
                    RecordTime: now,
                    Value: task.BonusPoints,
                    OrderNo: model.OrderNo,
                    Memo: model.Memo,
                    WorkId: self.data.WorkId
                }
                LobsterAPI.OrmMySql.SaveModel(self.data.dbConn, self.data.PointRecordModel);
            };
            if (task.BonusGrowth > 0) {
                self.data.GrowthRecordModel.objval = {
                    MemberId: info.MemberId,
                    TaskCode: task.TaskCode,
                    TaskName: task.TaskName,
                    RecordTime: now,
                    Value: task.BonusGrowth,
                    OrderNo: model.OrderNo,
                    Memo: model.Memo,
                    WorkId: self.data.WorkId
                }
                LobsterAPI.OrmMySql.SaveModel(self.data.dbConn, self.data.GrowthRecordModel);
            }
            info.MemberPoints = parseInt(info.MemberPoints) + parseInt(task.BonusPoints);
            LobsterAPI.TrackLog("会员积分", info.MemberPoints);
            info.GrowthValue = parseInt(info.GrowthValue) + parseInt(task.BonusGrowth);
            LobsterAPI.TrackLog("会员成长值", info.GrowthValue);
            strsql = "select * from Me_MemberLevel where WorkId=? order by sort";
            dbparams = Packages.java.util.ArrayList();
            dbparams.add(self.data.WorkId);
            var list = ORMFunction.GetJsonData(self.data.dbConn, strsql, dbparams);
            if (list.length > 0) {
                for (let i = 0; i < list.length; i++) {
                    if (info.GrowthValue >= list[i].StartGrowth && info.GrowthValue < list[i].EndGrowth) {
                        info.Level = i;
                        break;
                    }
                }
            }
            strsql = "update Me_BasicInfo set MemberPoints=?,GrowthValue=?,Level=? where MemberId=?";
            dbparams = Packages.java.util.ArrayList();
            dbparams.add(info.MemberPoints);
            dbparams.add(info.GrowthValue);
            dbparams.add(info.Level);
            dbparams.add(info.MemberId);
            var ret = self.data.dbConn.executeUpdate(strsql, dbparams);
            if (ret < 1)
                throw '更新会员失败';
            self.data.dbConn.commit();
        } catch (e) {
            self.data.dbConn.rollback();
            return { code: 1, msg: e.message };
        }
    },
    obtainCoupon: function (model) {
        var self = this;
        try {
            LobsterAPI.TrackLog("obtainCoupon", model);
            var list = JSON.parse(model.CouponIds);
            if (list.length == 0)
                return { code: 1, msg: "无此权益" };
            self.data.dbConn.setAutoCommit(false);
            var strsql = "select * from Me_BasicInfo where WorkId=? and UserId=?";
            var dbparams = Packages.java.util.ArrayList();
            dbparams.add(self.data.WorkId);
            dbparams.add(model.UserId);
            var info = ORMFunction.GetJsonObject(self.data.dbConn, strsql, dbparams);
            if (!info.MemberId)
                return { code: 1, msg: "找不到会员信息" };
            strsql = "select * from Me_CouponRecord where WorkId=? and MemberId=? and LevelId=? and RightId=?";
            dbparams = Packages.java.util.ArrayList();
            dbparams.add(self.data.WorkId);
            dbparams.add(info.MemberId);
            dbparams.add(model.LevelId);
            dbparams.add(model.RightId);
            var couponRecord = ORMFunction.GetJsonObject(self.data.dbConn, strsql, dbparams);
            if (couponRecord.RecordId > 0)
                return { code: 1, msg: "请不要重复领取" };

            list.forEach(item => {
                strsql = "select * from Me_Coupon where CouponId=?";
                var dbparams = Packages.java.util.ArrayList();
                dbparams.add(item);
                var coupon = ORMFunction.GetJsonObject(self.data.dbConn, strsql, dbparams);
                if (coupon.ValidType == 2) {
                    coupon.StartTime = self.getdatetime();
                    coupon.EndTime = self.getdatetime(self.adddate(coupon.ValidDays));
                    LobsterAPI.TrackLog("券开始时间-结束时间", coupon.StartTime + "-" + coupon.EndTime);
                }
                self.data.CouponRecordModel.objval = {
                    CouponId: coupon.CouponId,
                    CouponCode: "",
                    CouponName: coupon.CouponName,
                    MemberId: info.MemberId,
                    CouponClass: coupon.CouponClass,
                    CouponType: coupon.CouponType,
                    StartTime: coupon.StartTime,
                    EndTime: coupon.EndTime,
                    CouponAmount: coupon.CouponAmount,
                    CouponDiscount: coupon.CouponDiscount,
                    FullImpairment: coupon.FullImpairment,
                    CouponStatus: 0,
                    AdaptiveRange: coupon.AdaptiveRange,
                    WorkId: self.data.WorkId,
                    GetTime: self.getdatetime(new Date()),
                    OrderNo: "",
                    LevelId: model.LevelId,
                    RightId: model.RightId
                };
                LobsterAPI.OrmMySql.SaveModel(self.data.dbConn, self.data.CouponRecordModel);
            });
            self.data.dbConn.commit();
        } catch (e) {
            self.data.dbConn.rollback();
            return { code: 1, msg: e.message };
        }
    },

    registermember: function (model) {
        var self = this;
        try {
            self.data.dbConn.setAutoCommit(false);
            var strsql = "select * from Me_BasicInfo where WorkId=? and UserId=?";
            var dbparams = Packages.java.util.ArrayList();
            dbparams.add(self.data.WorkId);
            dbparams.add(model.UserId);
            var info = ORMFunction.GetJsonObject(self.data.dbConn, strsql, dbparams);
            if (info.MemberId > 0)
                return { code: 1, msg: "已经存在会员数据" };
            self.data.BasicInfoModel.objval = {
                NickName: model.NickName,
                Level: 0,
                MemberPoints: 0,
                UserId: model.UserId,
                CouponCounts: 0,
                GrowthValue: 0,
                PayingMember: 0,
                WorkId: self.data.WorkId
            };
            LobsterAPI.OrmMySql.SaveModel(self.data.dbConn, self.data.BasicInfoModel);
            self.data.dbConn.commit();
        } catch (e) {
            self.data.dbConn.rollback();
            return { code: 1, msg: e.message };
        }
    },
    getcouponlist: function (page, limit, userId) {
        var self = this;
        var pageInfo = {};
        pageInfo.key = "GetTime desc";
        pageInfo.page = page;
        pageInfo.perPage = limit;
        pageInfo.totalRecord = 0;
        pageInfo.totalPage = 0;
        var strsql = "select a.* from Me_CouponRecord a inner join Me_BasicInfo b on a.MemberId=b.MemberId where b.UserId=?";
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(userId);
        var data = ORMFunction.GetJsonData(self.data.dbConn, strsql, dbparams, pageInfo);
        return { code: 0, data: data, count: pageInfo.totalRecord };
    },
    getdate: function () {
        var self = this;
        var date = new Date();
        return date.getFullYear().toString() + "-" + self.pad2(date.getMonth() + 1) + "-" + self.pad2(date.getDate());
    },
    getdatetime: function (date) {
        var self = this;
        if (!date)
            date = new Date()
        return date.getFullYear().toString() + "-" + self.pad2(date.getMonth() + 1) + "-" + self.pad2(date.getDate())
            + " " + self.pad2(date.getHours()) + ":" + self.pad2(date.getMinutes()) + ":" + self.pad2(date.getSeconds());
    },
    pad2: function (n) { return n < 10 ? '0' + n : n + '' },
    adddate: function (days) {
        var dateTime = new Date();
        dateTime = dateTime.setDate(dateTime.getDate() + parseInt(days));
        return new Date(dateTime);
    }
})